<!-- Khu vực thêm module -->
<?php echo form_open('admin/setting/addUser',array('class'=>'form-horizontal'))?>

<div class="addform">
    <div class="menuUserManager">
      <button class="btn">Thêm người quản trị</button>
    </div>
    <br />
    <div class="thongbao">
    <!-- Nếu có lỗi thì in ra lỗi -->
    <?php if(isset($success) && $success){
    ?>
    <div class="alert alert-success fade in"><?php echo $success?><a class="close" data-dismiss="alert">&times;</a></div>
    <?php    
    }?>
    <?php if($error){?>
        <?php foreach($error as $ele):?>
        <div class="alert alert-error fade in"><?php echo $ele?><a class="close" data-dismiss="alert">&times;</a></div>
        <?php endforeach;?>
    <?php }?>
    </div>
    <div class="row">
        <div class="span6">
            <div class="control-group">
                <label class="control-label">Tài khoản đăng nhập</label>
                <div class="controls">
                    <?php echo form_input('adu_name')?>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label">Mật khẩu</label>
                <div class="controls">
                    <?php echo form_password('adu_pass')?>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label">Nhập lại mật khẩu</label>
                <div class="controls">
                    <?php echo form_password('adu_repass')?>
                </div>
            </div>   
            <div class="control-group">
                <label class="control-label">Địa chỉ</label>
                <div class="controls">
                    <?php echo form_input('adu_address')?>
                </div>
            </div> 
            <div class="control-group">
                <label class="control-label">Điện thoại</label>
                <div class="controls">
                    <?php echo form_input('adu_phone')?>
                </div>
            </div>
            <div class="span3">    
                <div class="control-group">
                    <label class="control-label">Quyền Admin</label>
                    <div class="controls">
                        <?php echo form_checkbox('adu_role')?>
                    </div>
                </div> 
            </div> 
        </div>
        <div class="span6">
            <div class="control-group">
                <label class="control-label">Họ & Tên</label>
                <div class="controls">
                    <?php echo form_input('adu_fullname')?>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label">Địa chỉ email</label>
                <div class="controls">
                    <?php echo form_input('adu_mail')?>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label">Yahoo</label>
                <div class="controls">
                    <?php echo form_input('adu_yahoo')?>
                </div>
            </div> 
            <div class="control-group">
                <label class="control-label">Skype</label>
                <div class="controls">
                    <?php echo form_input('adu_skype')?>
                </div>
            </div> 
            <div class="control-group">
                <label class="control-label">Ngày sinh</label>
                <div class="controls">
                    <?php echo form_input('adu_birthday')?>
                </div>
            </div> 
            <div class="span3">
                <div class="control-group">
                    <label class="control-label">Kích hoạt</label>
                    <div class="controls">
                        <?php echo form_checkbox('adu_active')?>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="form-actions">
        <button type="submit" class="btn btn-primary">Thêm mới</button>
        <input type="reset" class="btn" value="Hủy bỏ"/>
    </div>
</div>
<?php echo form_close()?>

<!-- Khu vực danh sách users + sửa xóa -->
<div class="menuUserManager">
  <button class="listuserBtn btn ">Danh sách quản trị viên</button>
</div>
<div id="mainManager">
    <div id="listuser">
    <?php foreach($list as $user):?>
        <div class="detailuser">
            <div style="display: none;">
                <?php echo form_hidden('adu_id',$user['adu_id'])?>
            </div>
            <div class="thongbao"></div>
            <div class="btn-toolbar">
                <div class="btn-group">
                    <a class="btn  <?php if(!$user['adu_active']) echo 'disabled'?>"><i class="icon-user"></i> <?php echo $user['adu_name']?></a>
                    <a class="btn dropdown-toggle" data-toggle="dropdown" href="#"><span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="#" class="editBtn"><i class="icon-pencil"></i> Edit</a></li>
                        <li><a href="#" class="deleteBtn"><i class="icon-trash"></i> Delete</a></li>
                        <li><?php if($user['adu_active']){?>
                            <a href="#" class="banBtn"><i class="icon-ban-circle"></i> Ban</a>
                            <?php } else{ ?>
                            <a href="#" class="activeBtn"><i class="icon-check"></i> Active</a>    
                            <?php }?>
                        </li>
                        <li class="divider"></li>
                        <li><?php if(!$user['adu_role']){?>
                            <a href="#" class="makeAdmin"> Make admin</a></li>
                            <?php }else{?>
                            <a href="#" class="cancelAdmin"> Cancel admin</a></li>
                            <?php }?>
                    </ul>
                </div>
            </div>
            <div class="row">
                <div class="span7">
                    <div class="row">
                        <div class="span2">Họ tên :</div><div class="span4"><?php echo $user['adu_fullname']?></div>
                    </div>
                    <div class="row">
                        <div class="span2">Email :</div><div class="span4"><?php echo $user['adu_mail']?></div>
                    </div>
                    <div class="row">
                        <div class="span2">Số điện thoại :</div><div class="span4"><?php echo $user['adu_phone']?></div>
                    </div>
                    <div class="row">
                        <div class="span2">Địa chỉ :</div><div class="span4"><?php echo $user['adu_address']?></div>
                    </div>
                    <div class="row">
                        <div class="span2">Yahoo :</div><div class="span4"><?php echo $user['adu_yahoo']?></div>
                    </div>
                    <div class="row">
                        <div class="span2">Skype :</div><div class="span4"><?php echo $user['adu_skype']?></div>
                    </div>
                    <div class="row">
                        <div class="span2">Birthday :</div><div class="span4"><?php echo $user['adu_birthday']?></div>
                    </div>
                </div>
                
                <div class="span7">    
                    <label class="control-label">Phân quyền quản trị</label>
                    <div class="row">
                        <div class="span5">
                            <div class="tabbable tabs-left roleManager">
                                <ul class="nav nav-tabs">
                                <?php foreach($modules as $mod):?>
                                    <li><a href="#<?php echo $mod['mod_id'].'u'.$user['adu_id']?>" data-toggle="tab"><?php echo $mod['mod_name']?></a></li>
                                <?php endforeach;?>
                                </ul>
                                <div class="tab-content">
                                <?php 
                                    //Lấy ra mảng permis của 1 user
                                    if(!$permission){
                                        echo 'Chưa có module';
                                        return false;
                                    }
                                    $permis = $permission[$user['adu_name']]; 
                                    
                                    //Per_id là id của module, per_value là mảng giá trị permission
                                    foreach($permis as $per_id=>$per_value):
                                ?>                        
                                    <div class="tab-pane" id="<?php echo $per_id.'u'.$user['adu_id']?>">
                                        <div class="control-group">
                                            <div class="controls">
                                                <label class="checkbox inline">
                                                    <input type="checkbox" value="1" <?php if($per_value['add']) echo 'checked';?> name="per_add"/>
                                                    Thêm
                                                </label>
                                                <label class="checkbox inline">
                                                    <input type="checkbox" value="1" <?php if($per_value['edit']) echo 'checked';?> name="per_edit"/>
                                                    Sửa
                                                </label>
                                                <label class="checkbox inline">
                                                    <input type="checkbox" value="1" <?php if($per_value['delete']) echo 'checked';?> name="per_delete"/>
                                                    Xóa
                                                </label>
                                            </div>
                                        </div>
                                    </div>
                                <?php endforeach;?>
                                </div>
                            </div>
                        </div><!--End tab-pane -->
                        <div class="span2">
                            <button class="saveBtn btn "><i class="icon-hdd"></i> Lưu</button>
                        </div>
                    </div><!-- End row -->
                </div>
            </div>
        </div><hr />
    <?php endforeach;?>
    </div>
</div>
<script>
//Active user
    $('.activeBtn').live('click',function(){
        //Xác định id của user
        var id = $(this).closest('.detailuser').find('input[name=adu_id]').val();
        quickEditUser(id,'active',this);
        return false;
    });
//inactive user
    $('.banBtn').live('click',function(){
        var id = $(this).closest('.detailuser').find('input[name=adu_id]').val();
        quickEditUser(id,'ban',this);
        return false;
    });
//Delete user
    $('.deleteBtn').live('click',function(){
        //Xác định id của user
        var id = $(this).closest('.detailuser').find('input[name=adu_id]').val();
        quickEditUser(id,'delete',this);
        return false;
    });

//Make admin
    $('.makeAdmin').live('click',function(){
        var id = $(this).closest('.detailuser').find('input[name=adu_id]').val();
        quickEditUser(id,'makeAdmin',this);
        return false;
    });
//Cancel admin
    $('.cancelAdmin').live('click',function(){
        var id = $(this).closest('.detailuser').find('input[name=adu_id]').val();
        quickEditUser(id,'cancelAdmin',this);
        return false;
    });    
    
//Phân quyền
$('.detailuser .nav').find('li:first').addClass('active');
$('.detailuser .tab-content').find('.tab-pane:first').addClass('active');
$('.detailuser .nav').find('li').click(function(){
    var $nav = $(this).closest('.nav');
    var $tabContent = $(this).closest('.detailuser').find('.tab-content');
    var height = $(this).height();
    var idTab = $($nav.find('li')).index(this);
    var marginTop = idTab*height;
    $tabContent.css('margin-top',marginTop);
    
});
$('.detailuser .saveBtn').click(function(){
    //Mảng dữ liệu truyền đi, dữ liệu là chuỗi json
    var data = new Array;
    //Xác định detail user hiện tại
    $userDiv = $(this).closest('.detailuser');
    $roleDiv = $userDiv.find('.roleManager');
    $ulNav = $roleDiv.find('.nav-tabs');
    $tabContent = $roleDiv.find('.tab-content');
    $thongbao = $userDiv.find('.thongbao');
    $tabContent.find('.tab-pane').each(function(){
        idString = $(this).attr('id');
        var addValue = ($(this).find('input[name=per_add]').attr('checked')) ? ($(this).find('input[name=per_add]').val()) : 0;
        var editValue = ($(this).find('input[name=per_edit]').attr('checked')) ? ($(this).find('input[name=per_edit]').val()) : 0;
        var deleteValue = ($(this).find('input[name=per_delete]').attr('checked')) ? ($(this).find('input[name=per_delete]').val()) : 0;
        value = {
                'add':addValue,
                'edit':editValue,
                'delete':deleteValue,
                }
        data.push({key:idString,value:value});
        
    });
    $.ajax({
        type:'post',
        url:'updatePermission',
        data:{'data':data},
        dataType:'json',
        beforeSend:function(){
            
        },
        success:function(json){
            if(json.error){
                $thongbao.html('<div class="alert alert-error fade in">'+json.error+'<a class="close" data-dismiss="alert">&times;</a></div>');
            }   
            else if(json.success){
                
                $thongbao.html('<div class="alert alert-success fade in">'+json.success+'<a class="close" data-dismiss="alert">&times;</a></div>');
            }
        }
    });
});
</script>